<?php
namespace app\install\controller;

use think\Controller;
use tp5_lib\install\logic\Module;
use tp5_lib\install\logic\DynamicConfig;
use DataWidget\widget\FormWidget;

/**
 * (单个)模块管理控制器
 */
class ModuleController extends Controller
{
    public function index()
    {
        $module=$this->getList()[$this->getName()];
        return $this->fetch('',$module);
    }
    public function getName()
    {
        if(empty($this->_name)){
            if(!$this->_name=input("module")){
                $this->error("模块ID不能为空");
            }
        }
        return $this->_name;
    }
    public function getList()
    {
        if(empty($this->_list)){
            if(!$this->_list=cache("modules")){
                $this->redirect("install/index/index");
            }
        }
        return $this->_list;
    }
    public function getModuleLogic()
    {
        $module=$this->getList()[$this->getName()];
        $class=$module["logic"];
        $logic=new $class($this->getName(),$this->getList());
        return $logic;
    }
    public function install()
    {
        $logic=$this->getModuleLogic();
        if(!$logic->install()){
            $this->error($logic->getError());
        }
        $this->success($this->getName() . "模块安装成功");
    }
    public function uninstall()
    {
        $logic=$this->getModuleLogic();
        if(!$logic->uninstall()){
            $this->error($logic->getError());
        }
        $this->success($this->getName() . "模块卸载成功");
    }
    public function clear()
    {
        $logic=$this->getModuleLogic();
        if(!$logic->clear()){
            $this->error($logic->getError());
        }
        $this->success($this->getName() . "模块清空成功");
    }
    public function update()
    {
        $logic=$this->getModuleLogic();
        if(!$logic->update()){
            $this->error($logic->getError());
        }
        $this->success($this->getName() . "模块更新成功");
    }
    public function forbidden()
    {
        $logic=$this->getModuleLogic();
        if(!$logic->forbidden()){
            $this->error($logic->getError());
        }
        $this->success($this->getName() . "模块已禁用");
    }
    public function unforbidden()
    {
        $logic=$this->getModuleLogic();
        if(!$logic->unforbidden()){
            $this->error($logic->getError());
        }
        $this->success($this->getName() . "模块已解除禁用");
    }
    public function config()
    {
        $name=$this->getName();
        $config=$this->getList()[$name]["config"];
        try {
            $values=DynamicConfig::alloc("modules")->read($name."_config");
        } catch (\Exception $exce) {
            $values=[];
        }
        $widget=FormWidget::alloc()
        ->setAction(url("submitConfig",["module"=>$name]))
        ->loadFields($config);
        if($values) $widget->setValues($values);
        $this->assign("form",$widget->toArray());
        return $this->fetch();
    }
    public function submitConfig()
    {
        $name=$this->getName();
        $config=$this->getList()[$name]["config"];
        try {
            $values=DynamicConfig::alloc("modules")->read($name."_config");
        } catch (\Exception $exce) {
            $values=[];
        }
        $widget=FormWidget::alloc()
        ->loadFields($config)
        ->setValues($values)
        ->input(input());
        // unset($data["module"]);
        DynamicConfig::alloc("modules")->write($this->getName()."_config", $widget->getValues());
        echo "配置成功";
    }
}
